﻿// ----------------------------------------------------------------------------------
// Microsoft Developer & Platform Evangelism
// 
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// ----------------------------------------------------------------------------------

namespace umbraco
{
    using System;
    using System.Linq;
    using System.Web.UI;
    using umbraco.cms.presentation.Trees;
    using umbraco.ACSExtensions;

    public partial class EditMemberGroupMapping : BasePages.UmbracoEnsuredPage
    {
        private MappingRuleService mappingRuleService = new MappingRuleService();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                var mappingId = this.Request.QueryString["mappingId"];
                var mapping = mappingRuleService.GetMappingRule(new Guid(mappingId));
                this.MainPanel.Text = "Business Identity Providers -> " + mapping.IdentityProvider.Name + " -> " + mapping.MemberGroup.Name + " -> " + mapping.Name;

                var acsManagementService = new AcsManagementService();

                this.ClaimTypes.DataSource = acsManagementService.RetrieveIdentityProviderClaimTypes(mapping.IdentityProvider.Name);
                this.ClaimTypes.DataBind();
                this.MappingId.Value = mappingId;
                this.ClaimTypes.Text = mapping.ClaimType;
                this.ClaimValue.Text = mapping.ClaimValue;
            }
        }

        private void Save_Click(object sender, ImageClickEventArgs e)
        {
            if (this.Page.IsValid)
            {
                var mapping = new MappingRule
                {   
                    Id = new Guid(this.MappingId.Value),
                    Name = this.ClaimTypes.SelectedValue.Split('/').Last(),
                    ClaimType = this.ClaimTypes.SelectedValue,
                    ClaimValue = this.ClaimValue.Text
                };

                this.mappingRuleService.EditMapping(mapping);
                this.ClientTools.ShowSpeechBubble(speechBubbleIcon.save, "Mapping Rule saved", string.Empty);
                
                // TODO: Refresh only the parent node (not the root node)
                ClientTools.RefreshTree(TreeDefinitionCollection.Instance.FindTree<loadMemberGroups>().Tree.Alias);
            }
        }

        protected override void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
            this.MainPanel.hasMenu = true;
            var saveButton = this.MainPanel.Menu.NewImageButton();
            saveButton.ImageUrl = UmbracoPath + "/images/editor/save.gif";
            saveButton.Click += new System.Web.UI.ImageClickEventHandler(this.Save_Click);
            saveButton.AlternateText = ui.Text("save");
        }

        private void InitializeComponent()
        {
        }
    }
}